package com.atguigu.gmall.list.service.impl;

import com.atguigu.gmall.feign.ProductFeignClient;
import com.atguigu.gmall.list.dao.GoodsDao;
import com.atguigu.gmall.list.service.GoodsService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/***
 * 商品搜索中商品管理使用的接口的实现类
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 把数据库中的数据添加到es中，通过skuid
     *
     * @param skuId
     */
    @Override
    public void addSkuFromDbToEsBySkuId(Long skuId) {
        //1 远程调用商品微服务的接口，拿到sku
        SkuInfo skuInfo = productFeignClient.getSkuInfoBySkuId(skuId);
        if(skuInfo == null || skuInfo.getId() == null){
            return;
        }

        //2 把查到的数据封装到Goods对象中
        Goods goods = new Goods();
        goods.setId(skuId);
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        //价格实时查
        BigDecimal price = productFeignClient.getSkuInfoPriceBySkuId(skuId);
        goods.setPrice(price.doubleValue());
        goods.setCreateTime(new Date());
        //获取品牌id
        Long tmId = skuInfo.getTmId();
        goods.setTmId(tmId);
        //查询品牌名字
        BaseTrademark baseTrademark = productFeignClient.getBaseTrademarkById(tmId);
        goods.setTmName(baseTrademark.getTmName());
        goods.setTmLogoUrl(baseTrademark.getLogoUrl());
        //获取一级、二级、三级分类信息
        BaseCategoryView baseCategoryView =
                productFeignClient.listCategoryByCategory3Id(skuInfo.getCategory3Id());
        goods.setCategory1Id(baseCategoryView.getCategory1Id());
        goods.setCategory1Name(baseCategoryView.getCategory1Name());
        goods.setCategory2Id(baseCategoryView.getCategory2Id());
        goods.setCategory2Name(baseCategoryView.getCategory2Name());
        goods.setCategory3Id(baseCategoryView.getCategory3Id());
        goods.setCategory3Name(baseCategoryView.getCategory2Name());
        //获取到指定商品的平台属性和平台属性值集合---TODO
        List<BaseAttrInfo> baseAttrInfoList = productFeignClient.listBaseAttrInfoBySkuId(skuId);
        //遍历，封装SearchAttr集合
        List<SearchAttr> searchAttrList = baseAttrInfoList.stream().map(baseAttrInfo -> {
            //封装searchAttr对象
            SearchAttr searchAttr = new SearchAttr();
            searchAttr.setAttrId(baseAttrInfo.getId());
            searchAttr.setAttrName(baseAttrInfo.getAttrName());
            searchAttr.setAttrValue(baseAttrInfo.getAttrValueList().get(0).getValueName());
            //返回
            return searchAttr;
        }).collect(Collectors.toList());
        goods.setAttrs(searchAttrList);

        //3 调用dao层方法，将数据存入es中
        goodsDao.save(goods);
    }

    /**
     * 删除指定skuId的商品es中的数据
     *
     * @param skuId
     */
    @Override
    public void removeGoodsFromEsBySkuId(Long skuId) {
        goodsDao.deleteById(skuId);
    }

    /**
     * 更新商品incrHotScore
     *
     * @param skuId
     */
    @Override
    public void incrHotScoreBySkuId(Long skuId) {
        //1 取出es中的数据
        Optional<Goods> optionalGoods = goodsDao.findById(skuId);
        if(optionalGoods.isPresent()){
            //数据存在，热度值+1,把HotScore的值存入redis中，到达一定数量同步到es中
            Double score = redisTemplate.opsForZSet().incrementScore("Goods_Hot_Score", skuId, 1);
            //每10次同步一次es
            if(score%10==0){
                Goods goods = optionalGoods.get();
                goods.setHotScore(score.longValue());
                goodsDao.save(goods);
            }
        }
    }
}
